package serveur;

import java.util.ArrayList;
import java.util.List;
import java.awt.Image;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.sql.ResultSetMetaData;
import java.util.Date;

import javax.swing.JOptionPane;

import mappingBD.*;

public class ServeurBD {

	public static Connection maConnexion = null;
	private PreparedStatement stmtSelect;
	private PreparedStatement stmtInsert;
	private PreparedStatement stmtInsert2;
	private PreparedStatement stmtUpdate;

	public ServeurBD() throws SQLException {
		maConnexion = ConnexionBD.getInstance();
	}

	public void executerRequeteServeur(Object obj) throws SQLException,
			Exception {
		String typeRequete;
		if (maConnexion != null) {
			if (obj instanceof Message) {
				typeRequete = ((Message) obj).getType();
				Message msg = ((Message) obj);
				if (typeRequete.equals("MESS")) {
					creerMessage(msg.getContenue(), msg
							.getClientQuiFaitRequete().getPseudo(), msg
							.getSalleCourante().getNomSalle());
					Synchronisation synchro = new Synchronisation(msg
							.getClientQuiFaitRequete());
					sync(synchro);
				} else if (typeRequete.equals("LIKE")) {
					likeDislikeMsg(msg.getApprecie(), msg.getIdMsg());
					Synchronisation synchro = new Synchronisation(msg
							.getClientQuiFaitRequete());
					sync(synchro);
				} else if (typeRequete.equals("MESD")) {
					supprimerMsg(msg.getClientQuiFaitRequete().getPseudo(),
							msg.getIdMsg());
					Synchronisation synchro = new Synchronisation(msg
							.getClientQuiFaitRequete());
					sync(synchro);
				}
			} else if (obj instanceof Client) {
				typeRequete = ((Client) obj).getType();

				Client client = ((Client) obj);
				if (typeRequete.equals("CONN")) {
					seConnecterServeur(client.getPseudo(),
							client.getMotDePasse());
					Synchronisation synchro = new Synchronisation(client);
					sync(synchro);
					
				} else if (typeRequete.equals("PRCR")) {
					creerProfil(client.getPseudo(), client.getNom(),
							client.getPrenom(), client.getMotDePasse(), null);
					Synchronisation synchro = new Synchronisation(client);
					sync(synchro);
				} else if (typeRequete.equals("PRMD")) {
					modifierProfil(client.getNom(), client.getPrenom(),
							client.getPseudo(), client.getMotDePasse());
					Synchronisation synchro = new Synchronisation(client);
					sync(synchro);
				} else if (typeRequete.equals("DECN")) {
					seDeconnecter(client.getPseudo());
					Synchronisation synchro = new Synchronisation(client);
					sync(synchro);
				}

			} else if (obj instanceof SalleDeDiscussion) {
				typeRequete = ((SalleDeDiscussion) obj).getType();
				SalleDeDiscussion salleCourante = ((SalleDeDiscussion) obj);
				if (typeRequete.equals("SDCR")) {
					creerSalle(salleCourante.getNomSalle(),
							salleCourante.getDescription(), salleCourante
									.getClientFaitRequete().getPseudo());
					
					Synchronisation synchro = new Synchronisation(salleCourante
							.getClientFaitRequete());
					sync(synchro);
					
				} else if (typeRequete.equals("SDDA")) {
					desAbonnerSalle(salleCourante.getClientFaitRequete()
							.getPseudo(), salleCourante.getNomSalle());
					Synchronisation synchro = new Synchronisation(salleCourante
							.getClientFaitRequete());
					sync(synchro);
				} else if (typeRequete.equals("SDQT")) {
					deconnecterSalle(salleCourante.getClientFaitRequete()
							.getPseudo(), salleCourante.getNomSalle());
					Synchronisation synchro = new Synchronisation(salleCourante
							.getClientFaitRequete());
					sync(synchro);
				} else if (typeRequete.equals("SDCN")) {
					deconnecterSalle(salleCourante.getClientFaitRequete()
							.getPseudo(), salleCourante.getNomSalle());
					Synchronisation synchro = new Synchronisation(salleCourante
							.getClientFaitRequete());
					sync(synchro);
				}
			} else if (obj instanceof Synchronisation) {
				Synchronisation synchronisation = ((Synchronisation) obj);
				sync(synchronisation);
			}

		}
		else
			System.out.println("Erreur connexion BD");
	}

	/* SECTION CLIENT */
	public void seConnecterServeur(String pseudo, String pwd)
			throws SQLException, Exception {
		if (validerAuthentification(pseudo, pwd))
			try {
				updateStatutClient(pseudo, 1);
				maConnexion.commit();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				maConnexion.rollback();
				throw e;
			}
		else
			System.out.println("authentification a echouee");
	}

	private boolean validerAuthentification(String pseudo, String pwd)
			throws SQLException {
		String passwd = "";
		if ((getIdClient(pseudo) != 0) && (pwd.length() > 0)) {
			stmtSelect = maConnexion
					.prepareStatement(("select motdepasse from Client where pseudo = ?"));
			stmtSelect.setString(1, pseudo);
			ResultSet rset = stmtSelect.executeQuery();
			if (rset.next()) {
				passwd = rset.getString("motdepasse");
			}
			rset.close();
			return (passwd.equals(pwd));
		}
		return false;
	}

	public void modifierProfil(String nom, String prenom, String pseudo,
			String passwd) throws SQLException {
		System.out.println("modifierProfil");
		try {
			stmtUpdate = maConnexion
					.prepareStatement("UPDATE client SET nomusager = ?, prenom = ?,  motdepasse = ?, datedernieremodif = ? "
							+ " WHERE pseudo = ?");
			stmtUpdate.setString(1, nom);
			stmtUpdate.setString(2, prenom);
			stmtUpdate.setString(3, passwd);
			stmtUpdate.setString(4, getDateCourante());
			stmtUpdate.setString(5, pseudo);
			// avatar
			stmtUpdate.executeUpdate();
			maConnexion.commit();
		} catch (SQLException e) {
			maConnexion.rollback();
			throw e;
		}

	}

	public void connecterSalle(String pseudo, String nomSalle)
			throws SQLException, Exception {
		System.out.println("4-connecterSalle");
		int idClient = getIdClient(pseudo);
		int idSalle = getIdSalle(nomSalle);
		try {
			if (idClient > 0 && idSalle > 0) {
				if (!estAbonneSalle(idClient, idSalle))
					abonnerSalle(idClient, idSalle);
				updateSalleClient(idClient, idSalle);
				maConnexion.commit();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (Exception e) {
			maConnexion.rollback();
			throw e;
		}
	}

	private void updateSalleClient(int idClient, int idSalle)
			throws SQLException, Exception {
		/* Mise a jour de la salle du client */
		stmtUpdate = maConnexion
				.prepareStatement("update Client set idSalle = ?, dateDerniereModif = ? "
						+ "where idClient = ?");
		stmtUpdate.setInt(1, idSalle);
		stmtUpdate.setString(2, getDateCourante());
		stmtUpdate.setInt(3, idClient);
		stmtUpdate.executeUpdate();
	}

	private boolean estAbonneSalle(int idClient, int idSalle)
			throws SQLException {
		stmtSelect = maConnexion
				.prepareStatement(("select idClient from Abonnement where idClient = ? and idSalle = ?"));
		stmtSelect.setInt(1, idClient);
		stmtSelect.setInt(2, idSalle);
		ResultSet rset = stmtSelect.executeQuery();
		if (rset.next()) {
			return true;
		}
		rset.close();
		return false;
	}

	public void abonnerSalle(int idClient, int idSalle) throws SQLException,
			Exception {
		stmtInsert = maConnexion
				.prepareStatement("insert into Abonnement(idClient, idSalle, date_abonnement) values (?,?,?)");
		stmtInsert.setInt(1, idClient);
		stmtInsert.setInt(2, idSalle);
		stmtInsert.setString(3, getDateCourante());
		stmtInsert.executeUpdate();
	}

	public void desAbonnerSalle(String pseudo, String nomSalle)
			throws SQLException {
		System.out.println("desAbonnerSalle");
		int idClient = getIdClient(pseudo);
		try {
			if (idClient > 0) {
				stmtUpdate = maConnexion
						.prepareStatement("DELETE FROM abonnement WHERE idclient = ?");
				stmtUpdate.setInt(1, idClient);

				stmtUpdate.executeUpdate();
				maConnexion.commit();
			}

		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

	public int getIdClient(String pseudo) throws SQLException {
		int idClient = 0;
		stmtSelect = ConnexionBD.getInstance().prepareStatement(
				("select idClient from Client where pseudo = ?"));
		stmtSelect.setString(1, pseudo);
		ResultSet rset = stmtSelect.executeQuery();
		if (rset.next()) {
			idClient = rset.getInt("idClient");

		}
		rset.close();
		return idClient;
	}

	private int getIdSalle(String nomSalle) throws SQLException {
		int idSalle = 0;
		stmtSelect = maConnexion
				.prepareStatement(("select idSalle from SalleDeDiscussion where nomSalle = ?"));
		stmtSelect.setString(1, nomSalle);
		ResultSet rset = stmtSelect.executeQuery();
		if (rset.next()) {
			idSalle = rset.getInt("idSalle");
		}
		rset.close();
		return idSalle;
	}

	/* SECTION MESSAGES */
	public void creerMessage(String contenuMsg, String pseudo, String nomSalle)
			throws SQLException, Exception {
		System.out.println("1-creerMessage");
		int idClient = getIdClient(pseudo);
		int idSalle = getIdSalle(nomSalle);
		try {
			if (idClient > 0 && idSalle > 0) {
				ajoutMessage(contenuMsg, idClient, idSalle);
				maConnexion.commit();
			}
		} catch (Exception e) {
			maConnexion.rollback();
			throw e;
		}
	}

	private void ajoutTransmission(int idClient, int idSalle, int idMsg)
			throws SQLException, Exception {
		stmtInsert2 = maConnexion
				.prepareStatement("insert into Transmission(idClient, idSalle, idMsg) values (?,?,?)");
		stmtInsert2.setInt(1, idClient);
		stmtInsert2.setInt(2, idSalle);
		stmtInsert2.setInt(3, idMsg);
		stmtInsert2.executeUpdate();
	}

	private void ajoutMessage(String contenuMsg, int idClient, int idSalle)
			throws Exception {
		Integer idMsg = 0;
		stmtInsert = maConnexion
				.prepareStatement("insert into Message(contenu, nblike, nbdislike, estSupprime,"
						+ " datecreation, datedernieremodif) values (?,?,?,?,?,?)");
		stmtInsert.setString(1, contenuMsg);
		stmtInsert.setInt(2, 0);
		stmtInsert.setInt(3, 0);
		stmtInsert.setInt(4, 0);
		stmtInsert.setString(5, getDateCourante());
		stmtInsert.setString(6, getDateCourante());
		stmtInsert.executeUpdate();
		ResultSet rset = stmtInsert.getGeneratedKeys(); // cause p-e le probleme
		if (rset.next()) {
			idMsg = rset.getInt(1);
			rset.close();
			ajoutTransmission(idClient, idSalle, idMsg); // cause p-e le
															// probleme
		}
	}

	public void deconnecterSalle(String pseudo, String nomSalle)
			throws SQLException {
		System.out.println("deconnecterSalle");
		int idClient = getIdClient(pseudo);

		try {
			if (idClient > 0) {
				stmtUpdate = maConnexion
						.prepareStatement("UPDATE client SET idsalle = 0, dateDerniereModif = ? WHERE pseudo = ?");
				stmtUpdate.setString(1, getDateCourante());
				stmtUpdate.setString(2, pseudo);
				stmtUpdate.executeUpdate();
				maConnexion.commit();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

	public void seDeconnecter(String pseudo) throws Exception {
		System.out.println("seDeconnecter");
		try {
			updateSalleClient(getIdClient(pseudo), 0);
			updateStatutClient(pseudo, 0);
			maConnexion.commit();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			maConnexion.rollback();
			throw e;
		}
	}

	
	public void creerProfil(String pseudo, String nom, String prenom,
			String passwd, Image avatar) throws SQLException, Exception {
		try {
			/* Verifie si le pseudo existe deja */
			if (getIdClient(pseudo) == 0) {
				/* Ajout du profil dans la table des clients */
				ajoutProfil(pseudo, nom, prenom, passwd, avatar);
				updateStatutClient(pseudo, 1);
				maConnexion.commit();
			} else
				System.out.println("Ce pseudo n est pas disponible. Veuillez changer");
		} catch (Exception e) {
			maConnexion.rollback();
			throw e;
		}
	}

	private void updateStatutClient(String pseudo, int statut)
			throws SQLException, Exception {
		/* Mise a jour du statut */
		stmtUpdate = maConnexion
				.prepareStatement("update Client set statutConnecte = ?, dateDerniereModif = ? "
						+ "where pseudo = ?");
		stmtUpdate.setInt(1, statut); // 1 = connecte 0 = deconnecte
		stmtUpdate.setString(2, getDateCourante());
		stmtUpdate.setString(3, pseudo);
		stmtUpdate.executeUpdate();
	}

	
	private void ajoutProfil(String pseudo, String nom, String prenom,
			String passwd, Image avatar) throws SQLException, Exception {
		stmtInsert = maConnexion
				.prepareStatement("insert into Client(nomusager, prenom, pseudo, motdepasse, statutConnecte, idsalle,"
						+ " datecreation, datedernieremodif) values (?,?,?,?,?,?,?,?)");
		stmtInsert.setString(1, nom);
		stmtInsert.setString(2, prenom);
		stmtInsert.setString(3, pseudo);
		stmtInsert.setString(4, passwd);
		// avatar ici
		stmtInsert.setBoolean(5, false);
		stmtInsert.setInt(6, 0);
		stmtInsert.setString(7, getDateCourante());
		stmtInsert.setString(8, getDateCourante());
		stmtInsert.executeUpdate();
	}

	
	public void creerSalle(String nomSalle, String descrip, String createur)
			throws SQLException, Exception {
		System.out.println("3-creerSalle");
		try {
			stmtInsert = maConnexion
					.prepareStatement("insert into SalleDeDiscussion(idClient, nomSalle, descriptionSalle,"
							+ " datecreation, datedernieremodif) values (?,?,?,?,?)");
			int idClient = getIdClient(createur);
			if (idClient > 0 && getIdSalle(nomSalle) == 0 ) {
				stmtInsert.setInt(1, idClient);
				stmtInsert.setString(2, nomSalle);
				stmtInsert.setString(3, descrip);
				stmtInsert.setString(4, getDateCourante());
				stmtInsert.setString(5, getDateCourante());
				stmtInsert.executeUpdate();
				maConnexion.commit();
				abonnerSalle(idClient, getIdSalle(nomSalle));
				maConnexion.commit();
			}
		} catch (Exception e) {
			maConnexion.rollback();
			throw e;
		}

	}

	public void supprimerMsg(String pseudo, int idMessage) throws SQLException {
		System.out.println("supprimerMsg");
		int idClient = getIdClient(pseudo);
		// verifier que le message provient de l'utilisateur
		if (idClient > 0) {
			stmtUpdate = maConnexion
					.prepareStatement(("UPDATE message SET estsupprime = 1, datedernieremodif = ? WHERE idmsg = ?"));
			stmtUpdate.setInt(2, idMessage);
			stmtUpdate.setString(1, getDateCourante());
			stmtUpdate.executeUpdate();
			maConnexion.commit();
		}

	}

	public void likeDislikeMsg(String apprecie, int idMessage)
			throws SQLException, Exception {
		System.out.println("6-likeDislikeMsg");
		try {
			if (apprecie.equals("1"))
				updateNbLikeMessage(idMessage);
			else
				updateNbDislikeMessage(idMessage);
			maConnexion.commit();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (Exception e) {
			maConnexion.rollback();
			throw e;
		}
	}

	private void updateNbDislikeMessage(int idMessage) throws SQLException {
		/* Mise a jour du nombre de like */
		int nbDislike = getStatsMessage(0, idMessage);
		stmtUpdate = maConnexion
				.prepareStatement("update Message set nbDisLike = ?, dateDerniereModif = ? "
						+ "where idMsg = ?");
		stmtUpdate.setInt(1, nbDislike + 1);
		stmtUpdate.setString(2, getDateCourante());
		stmtUpdate.setInt(3, idMessage);
		stmtUpdate.executeUpdate();
	}

	private void updateNbLikeMessage(int idMessage) throws SQLException {
		/* Mise a jour du nombre de dislike */
		int nbLike = getStatsMessage(1, idMessage);
		stmtUpdate = maConnexion
				.prepareStatement("update Message set nbLike = ?, dateDerniereModif = ? "
						+ "where idMsg = ?");
		stmtUpdate.setInt(1, nbLike + 1);
		stmtUpdate.setString(2, getDateCourante());
		stmtUpdate.setInt(3, idMessage);
		stmtUpdate.executeUpdate();
	}

	private int getStatsMessage(int type, int idMessage) throws SQLException {
		int donnee = 0;
		switch (type) {
		case 0:
			stmtSelect = maConnexion
					.prepareStatement(("select nbDisLike from Message where idMsg = ?"));
			break;
		case 1:
			stmtSelect = maConnexion
					.prepareStatement(("select nbLike from Message where idMsg = ?"));
			break;
		}
		stmtSelect.setInt(1, idMessage);
		ResultSet rset = stmtSelect.executeQuery();
		if (rset.next()) {
			donnee = rset.getInt(1);
		}
		rset.close();
		return donnee;
	}


	/* SECTION SYNC */

	public void sync(Synchronisation synchro) {
		try {
			Synchronisation retour = new Synchronisation(
					creerClientFaitRequete(synchro.getClientFaitRequete()), new Date(),
					creerListeClients(synchro.getClientFaitRequete()),
					creerListeDeSD());
			try {
				Emission emission = new Emission();
				emission.setSync(retour);
				Thread t4 = new Thread(emission);
				t4.start();
			} catch (IOException e) {
				e.printStackTrace();
			}

		} catch (SQLException e) {
			e.printStackTrace();
		}

		// return new Synchronisation(synchro.getClientFaitRequete());
	}
	public Client creerClientFaitRequete (Client emetteur) throws SQLException
	{
		ArrayList<String> temp = new ArrayList<String>();
		boolean estConnecte;

		// Requete permettant de recuperer les informations
		stmtSelect = maConnexion
				.prepareStatement(("SELECT nomusager,prenom,pseudo,motdepasse,statutconnecte FROM client WHERE pseudo = ?"));
		stmtSelect.setString(1, emetteur.getPseudo());
		ResultSet rset = stmtSelect.executeQuery();
		ResultSetMetaData resultMeta = rset.getMetaData();
		// Recuperation des donnees dans un tableau de Client
		while (rset.next()) {
			for (int i = 1; i <= resultMeta.getColumnCount(); i++) {
				temp.add(rset.getObject(i).toString());
			}

			if (temp.get(3).equals('0')) {
				estConnecte = false;
			}

			else {
				estConnecte = true;
			}
			;

			Client Clienttemp = new Client("", temp.get(0), temp.get(1),
					temp.get(2), temp.get(3), estConnecte);
			
			return Clienttemp;
		}

		return new Client("","","");

		
	}
	// Creation de la liste des clients qui utilisent le logiciel
	public ArrayList<Client> creerListeClients(Client clientRequete)
			throws SQLException {
		ArrayList<Client> listeARenvoyer = new ArrayList<Client>();
		ArrayList<String> temp = new ArrayList<String>();
		boolean estConnecte;

		// Requete permettant de recuperer les informations
		stmtSelect = maConnexion
				.prepareStatement(("SELECT nomusager,prenom,pseudo,statutconnecte FROM client WHERE pseudo != ?"));
		stmtSelect.setString(1, clientRequete.getPseudo());
		ResultSet rset = stmtSelect.executeQuery();
		ResultSetMetaData resultMeta = rset.getMetaData();
		// Recup�ration des donn�es dans un tableau de Client
		while (rset.next()) {
			for (int i = 1; i <= resultMeta.getColumnCount(); i++) {
				//System.out.println(rset.getObject(i));
				temp.add(rset.getObject(i).toString());
			}

			if (temp.get(3).equals('0')) {
				estConnecte = false;
			}

			else {
				estConnecte = true;
			}
			;

			Client Clienttemp = new Client("", temp.get(0), temp.get(1),
					temp.get(2), estConnecte);
			temp.clear();
			listeARenvoyer.add(Clienttemp);

		}

		return listeARenvoyer;

	}

	// Creation de la liste des salles de discussion
	public ArrayList<SalleDeDiscussion> creerListeDeSD() throws SQLException {
		int idSalle;
		ArrayList<SalleDeDiscussion> aRetourne = new ArrayList<SalleDeDiscussion>();
		ArrayList<String> temp = new ArrayList<String>();
		// Requete SQL
		stmtSelect = maConnexion
				.prepareStatement(("SELECT s.idsalle, s.nomsalle, s.descriptionsalle, c.pseudo "
						+ "FROM  salledediscussion s, client c "
						+ "WHERE s.idclient = c.idclient"));
		ResultSet rset = stmtSelect.executeQuery();
		ResultSetMetaData resultMeta = rset.getMetaData();

		// Recuperation des donnees de la requete
		// Boucle qui creer les objets messages
		while (rset.next()) {
			for (int i = 1; i <= resultMeta.getColumnCount(); i++) {
				//System.out.println(rset.getObject(i));
				temp.add(rset.getObject(i).toString());
			}

			idSalle = Integer.parseInt(temp.get(0));
			SalleDeDiscussion salleTemp = new SalleDeDiscussion(temp.get(1),
					temp.get(2), creerListeClientsSalle(idSalle),
					creerListeMessage(idSalle), temp.get(3));
			aRetourne.add(salleTemp);
			temp.clear();

		}
		return aRetourne;
	}

	public List<Message> creerListeMessage(int idSalle) throws SQLException {
		List<Message> aRetourne = new ArrayList<Message>();
		String contenu;
		int idMessage;
		boolean estSupprime;
		String pseudoAuteur;
		ArrayList<String> temp = new ArrayList<String>();

		// Requete SQL
		stmtSelect = maConnexion
				.prepareStatement("SELECT m.contenu,m.idmsg,m.estsupprime,c.pseudo "
						+ "FROM client c, transmission t, message m "
						+ "WHERE t.idclient = c.idclient "
						+ "AND t.idmsg = m.idmsg "
						+ "AND t.idsalle = ? "
						+ "ORDER BY t.idmsg");
		stmtSelect.setInt(1, idSalle);
		ResultSet rset = stmtSelect.executeQuery();
		ResultSetMetaData resultMeta = rset.getMetaData();

		// Boucle qui cree les objets messages
		while (rset.next()) {
			for (int i = 1; i <= resultMeta.getColumnCount(); i++) {

				temp.add(rset.getObject(i).toString());
			}

			contenu = temp.get(0);
			idMessage = Integer.parseInt(temp.get(1));
			estSupprime = (Integer.parseInt(temp.get(2)) == 1);
			pseudoAuteur = temp.get(3);
			Message messageTemporaire = new Message(contenu, idMessage,
					estSupprime, pseudoAuteur);
			aRetourne.add(messageTemporaire);
			temp.clear();

		}

		return aRetourne;
	}

	public ArrayList<Client> creerListeClientsSalle(int idSalle) throws SQLException {
		ArrayList<Client> listeARenvoyer = new ArrayList<Client>();
		ArrayList<String> temp = new ArrayList<String>();
		boolean estConnecte;

		// Requete permettant de recuperer les informations
		stmtSelect = maConnexion
				.prepareStatement(("SELECT c.idclient,c.nomusager,c.prenom,c.pseudo, c.statutconnecte "
						+ "FROM client c WHERE idsalle = ?"));
		stmtSelect.setInt(1, idSalle);
		ResultSet rset = stmtSelect.executeQuery();
		ResultSetMetaData resultMeta = rset.getMetaData();
		// Recuperation des donnees dans un tableau de Client
		while (rset.next()) {
			for (int i = 1; i <= resultMeta.getColumnCount(); i++) {
				temp.add(rset.getObject(i).toString());
			}

			if (temp.get(3).equals('0')) {
				estConnecte = false;
			}

			else {
				estConnecte = true;
			}
			;

			Client Clienttemp = new Client("", temp.get(0), temp.get(1),
					temp.get(2), estConnecte);
			temp.clear();
			listeARenvoyer.add(Clienttemp);

		}

		return listeARenvoyer;
	}

	/* FIN SECTION SYNC */

	/* GENERAL */
	private String getDateCourante() {
		Date dNow = new Date();
		SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		return ft.format(dNow);
	}

	public void fermerConnexionBD() {
		if (maConnexion != null) {
			try {
				maConnexion.close();
				JOptionPane.showMessageDialog(null, "Deconnexion de la BD",
						"FIN DE L'OPERATION", JOptionPane.ERROR_MESSAGE);
			} catch (SQLException e) {
				JOptionPane.showMessageDialog(null, e.getMessage(),
						"ECHEC DE LA DECONNEXION ", JOptionPane.ERROR_MESSAGE);
			}
		}
	}

}